package mylittleinvestigations.CaseInformation;

import java.io.IOException;
import java.util.*;
import mylittleinvestigations.DialogCutscenes.*;

public class DialogCutsceneManager
{
    private HashMap<String, DialogCutsceneAnimation> idToAnimationHashMap;
    private HashMap<String, DialogCutsceneAnimationElement> idToElementHashMap;

    public DialogCutsceneManager()
    {
        this.idToAnimationHashMap = new HashMap<String, DialogCutsceneAnimation>();
        this.idToElementHashMap = new HashMap<String, DialogCutsceneAnimationElement>();
    }

    public void AddAnimation(String id, DialogCutsceneAnimation animation)
    {
        this.idToAnimationHashMap.put(id, animation);
    }

    public void AddElement(String id, DialogCutsceneAnimationElement element)
    {
        this.idToElementHashMap.put(id, element);
    }

    public DialogCutsceneAnimation GetAnimationFromId(String id)
    {
        return this.idToAnimationHashMap.get(id);
    }

    public DialogCutsceneAnimationElement GetElementFromId(String id)
    {
        return this.idToElementHashMap.get(id);
    }

    void SaveToXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("DialogCutsceneManager");
        
        writer.StartElement("IdToAnimationHashMap");
        String[] ids = this.idToAnimationHashMap.keySet().toArray(new String[0]);
        
        for (int i = 0; i < ids.length; i++)
        {
            writer.StartElement("Entry");
            String id = ids[i];
            writer.WriteTextElement("Id", id);
            this.idToAnimationHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.StartElement("IdToElementHashMap");
        ids = this.idToElementHashMap.keySet().toArray(new String[0]);
        
        for (int i = 0; i < ids.length; i++)
        {
            writer.StartElement("Entry");
            String id = ids[i];
            writer.WriteTextElement("Id", id);
            this.idToElementHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.EndElement();
    }

    public DialogCutsceneManager(CaseXmlReader reader)
    {
        this.idToAnimationHashMap = new HashMap<String, DialogCutsceneAnimation>();
        this.idToElementHashMap = new HashMap<String, DialogCutsceneAnimationElement>();
        
        reader.StartElement("DialogCutsceneManager");
        
        reader.StartElement("IdToAnimationHashMap");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            DialogCutsceneAnimation animation = new DialogCutsceneAnimation(reader);
            
            this.idToAnimationHashMap.put(id, animation);
        }
        
        reader.EndElement();
        
        reader.StartElement("IdToElementHashMap");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            DialogCutsceneAnimationElement element = new DialogCutsceneAnimationElement(reader);
            
            this.idToElementHashMap.put(id, element);
        }
        
        reader.EndElement();
        
        reader.EndElement();
    }
}
